 #include<iostream>
#include<string>
#include"contacts.pb.h"
#include<fstream>
using namespace std;
void AddPeopleInfo(contacts2::PeopleInfo* pcont)
{
    string name;
    cout<<"-----新增联系人------"<<endl;
    cout<<"请输入姓名：";
    getline(cin,name);
    pcont->set_name(name);
    cout<<"请输入年龄：";
    int age;
    cin>>age;
    pcont->set_age(age);
    cin.ignore(256,'\n');
    for(int i =0;;i++)
    {
        cout<<"请输入联系电话"<<i+1<<":(只输入回车完成新增)";
        string number;
        getline(cin,number);
        if(number.empty())
        {
            break;
        }
        contacts2::PeopleInfo_Phone* phone = pcont->add_phione();
        phone->set_number(number);
        cout<<"请输入电话类型(1.移动电话 2.固定电话):";
        int type;
        cin>>type;
        cin.ignore(256,'\n');
        switch(type)
        {
            case 1:
                phone->set_type(contacts2::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
                break;
            case 2:
                phone->set_type(contacts2::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
                break;
            default:
                cout<<"输入有误"<<endl;
                break;    
        }
        

    }
    contacts2::Address address;
        cout<<"请输入联系人家庭地址：";
        string homeadd;
        getline(cin,homeadd);
        address.set_home_address(homeadd);
        cout<<"请输入联系人单位地址：";
        string unitadd;
        getline(cin,unitadd);
        address.set_unit_address(unitadd);
        google::protobuf::Any* data = pcont->mutable_data();
        data->PackFrom(address);
        cout<<"请选择联系方式：1.qq 2.wechat";
        int other_contact;
        cin>>other_contact;
        cin.ignore(256,'\n');
        if(1==other_contact)
        {
            cout<<"请输入qq: ";
            string qq;
            getline(cin,qq);
            pcont->set_qq(qq);
        }
        else if(2==other_contact)
        {
             cout<<"请输入wechat: ";
            string wechat;
            getline(cin,wechat);
            pcont->set_wechat(wechat);
        }
        else{
            cout<<"选择错误"<<endl;
        }
        for(int i=1;;i++)
        {
            cout<<"请输入备注"<<i<<"标签：";
            string remark_key;
            getline(cin,remark_key);
            if(remark_key.empty())
            {
                break;
            }
            cout<<"请输入备注"<<i<<"内容：";
            string remark_value;
            getline(cin,remark_value);
            pcont->mutable_remake()->insert({remark_key,remark_value});
        }
    cout<<"添加联系人成功"<<endl;
}

int main()
{
    fstream input("test.bin",ios::in|ios::binary);
    contacts2::PeopleContacts conts;
    if(!input)
    {
        cerr<<"file not find,creat new file"<<endl;
    }
    else if(!conts.ParseFromIstream(&input))
    {
        cerr<<"failed to parse contacts"<<endl;
        input.close();
        return -1;
    }
    AddPeopleInfo(conts.add_cont());
    fstream output("test.bin",ios::out|ios::trunc|ios::binary);
    if(!conts.SerializeToOstream(&output))
    {
        cerr<< "filed to write contacts"<<endl;
        input.close();
        output.close();
        return -1;
    }
    input.close();
    output.close();
    return 0;
}